<?php

class TrangChu_IndexController extends BaseController
{

	
	public function __construct()
	{
		parent::__construct();
		$this->_children[] = new Request('trang-chu/index/nav-header',array('trang-chu'));
	}
	
	public function indexAction() 
	{
// 		echo "<pre>";
// 		print_r($_SESSION["fb_user"]);
// 		echo "</pre>";
// 		exit();
		
		
	    $this->renderView('trang-chu/index/index');
	}
	
	public function dieuKhoanBaoMatAction()
	{
		$this->renderView('trang-chu/index/dieu-khoan-bao-mat');
	}
	
	public function navHeaderAction($current_nav = "trang-chu")
	{
		$menu_active = array();
		$menu_active[$current_nav] = " class='active'";
	    $this->oView->menu_active = $menu_active;

	    $objCategory = new Amway_Category();
	    $rsCategory = $objCategory->getRowset("active = 1");
	    
	    $this->oView->rsCategory = $rsCategory;	    
	    
	    return $this->oView->fetch('trang-chu/index/header');
	}
	
	public function commonAction()
	{
		$configFacebook = array(
				"facebook_appid" => $this->oConfigureSystem['facebook_appid'],
				"facebook_canvas_url" => $this->oConfigureSystem['facebook_canvas_url']);
		
		$this->oView->configFacebook = $configFacebook;
		
// 		echo "<pre>";
// 		print_r($_SESSION["fb_user"]);
// 		echo "</pre>";
// 		exit();
// 		unset($_SESSION["fb_user"]);

// 		echo "<pre>";
// 		print_r($_SESSION["fb_user"]);
// 		echo "</pre>";
// 		exit();
		
		if (empty($_SESSION["fb_user"]['id'])) 
		{
			$this->oView->have_permisstion = FALSE;
			$this->oView->fb_user = NULL;
		} else 
		{
			$this->oView->have_permisstion = TRUE;
			$this->oView->fb_user = $_SESSION["fb_user"];
		}
		
	}
	
	public function loadFbUserAction()
	{
// 		https://www.facebook.com/app_scoped_user_id/808258752519605/
		$array_fb_conf = array (
				'appId' => $this->oConfigureSystem['facebook_appid'],
				'secret' => $this->oConfigureSystem['facebook_appsecret'],
				'cookie' => true,
				'xfbml' => true,
				'oauth' => true
		);
		$facebook = new Facebook($array_fb_conf);
		$arrFbData = $facebook->getSignedRequest();
		
		$currentFbID = $facebook->getUser();
		
		if ($currentFbID)
		{
			$objMember = new Member();
			$rowMember = $objMember->getRow("fbid = ?", array($currentFbID));
			if ($rowMember)
			{
				$arr = array(
						"id" => $rowMember["id"],
						"fbid" => $rowMember["fbid"],
						"fullname" => $rowMember["fullname"],
						"gender" => $rowMember["gender"],
						"email" => $rowMember["email"],
						"first_name" => $rowMember["first_name"],
						"last_name" => $rowMember["last_name"]
						//"username" => $rowMember["username"] khong lay duoc username
				);
				
			}
			else
			{
				//$inPath = "http://graph.facebook.com/{$fbid}/picture?type=large";				
				$fbme = $facebook->api("/".$currentFbID);
				
				$arr = array(
						"fbid" => $fbme["id"],
						"fullname" => $fbme["name"],
						"gender" => $fbme["gender"],
						"email" => $fbme["email"],
						"first_name" => $fbme["first_name"],
						"last_name" => $fbme["last_name"]
						//"username" => $fbme["username"] khong lay duoc username
				);
				
				$current_id = $objMember->insert($arr);
				$arr['id'] = $current_id;
			}
			
// 			echo "<pre>";
// 			print_r($fbme);
// 			echo "</pre>";
			
			$_SESSION["fb_user"] = $arr;
// 			$this->oView->have_permisstion = TRUE;

			echo json_encode($arr);
		}
		
		exit();
	}
	
	
	
	public function fbUserAction()
	{
		$array_fb_conf = array (
				'appId' => $this->oConfigureSystem['facebook_appid'],
				'secret' => $this->oConfigureSystem['facebook_appsecret'],
				'cookie' => true,
				'xfbml' => true,
				'oauth' => true
		);
		$facebook = new Facebook($array_fb_conf);
// 		$arrFbData = $facebook->getSignedRequest();
	
		//$inPath = "http://graph.facebook.com/{$fbid}/picture?type=large";
		$fbme = $facebook->api("/"."808258752519605");
		
		echo "<pre>";
		print_r($fbme);
		echo "</pre>";
		exit();
		
		// We have a user ID, so probably a logged in user.
		// If not, we'll get an exception, which we handle below.
// 		try {
// 		$fql = 'SELECT id,name,gender,email,first_name,last_name,username from user where uid = ' . "808258752519605";
// 				$ret_obj = $facebook->api(array(
// 						'method' => 'fql.query',
// 						'query' => $fql,
// 				));
		
// 						// FQL queries return the results in an array, so we have
// 								//  to get the user's name from the first element in the array.
// 		echo '<pre>Name: ' . $ret_obj[0]['name'] . '</pre>';
		
// 		echo "<pre>";
// 		print_r($ret_obj);
// 		echo "</pre>";
// 		exit();
		
// 		} catch(FacebookApiException $e) {
// 		// If the user is logged out, you can have a
//         // user ID even though the access token is invalid.
// 		        // In this case, we'll get an exception, so we'll
// 			// just ask the user to login again here.
// 			$login_url = $facebook->getLoginUrl();
// 			echo 'Please <a href="' . $login_url . '">login.</a>';
// 			error_log($e->getType());
// 			error_log($e->getMessage());
// 		}		
		
		echo "<pre>";
// 		print_r($fbme);
		echo "</pre>";
		exit();
	
	}
	

}
